<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Text Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/helpers/text_helper.html
 */
// ------------------------------------------------------------------------
/**
 * chs_word_limiter
 *
 * 截取中间的一段文本
 *
 * @access	public
 * @param	string  $str        文本内容
 * @param	int     $start      开始位置    默认 0
 * @param	int     $lenth      字数        默认 10
 * @return	string
 */	
if ( ! function_exists('word_limiter')){
	function word_limiter($str, $lenth = 10, $start = 0, $end_char = '...' ,$start_char = '' ){
        $len = strlen($str);
        $r = array();
        $n = 0;
        $m = 0;
        for($i = 0; $i < $len; $i++) {
            $x = substr($str, $i, 1);
            $a = base_convert(ord($x), 10, 2);
            $a = substr('00000000'.$a, -8);
            if ($n < $start){
                if (substr($a, 0, 1) == 0) {
                }elseif (substr($a, 0, 3) == 110) {
                    $i += 1;
                }elseif (substr($a, 0, 4) == 1110) {
                    $i += 2;
                }
                $str.=$start_char.$str;
                $n++;
            }else{
                if (substr($a, 0, 1) == 0) {
                    $r[] = substr($str, $i, 1);
                }else if (substr($a, 0, 3) == 110) {
                    $r[] = substr($str, $i, 2);
                    $i += 1;
                }else if (substr($a, 0, 4) == 1110) {
                    $r[] = substr($str, $i, 3);
                    $i += 2;
                }else{
                    $r[] = '';
                }
                if (++$m >= $lenth){
                    break;
                }
            }
        }
        $this_text = join($r);
        if ($start!=0){
			$this_text = $start_char.$this_text;
        }
        if ($lenth-$i<=-1){
        	$this_text = $this_text.$end_char;
        }
        return $this_text;
        
	}
}
/* End of file text_helper.php */
/* Location: ./system/helpers/text_helper.php */